package com.kdd.campus.sh.controller;

import com.kdd.campus.common.entity.CommonResult;
import com.kdd.campus.common.util.RedisUtil;
import com.kdd.campus.sh.dto.*;
import com.kdd.campus.sh.service.OrderService;
import com.kdd.campus.sh.util.JwtTokenUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/****
 * @Author:shenkunlin
 * @Description:
 * @Date 2019/6/14 0:18
 *****/
@Api(value = "订单")
@RestController
@RequestMapping("/order")
@CrossOrigin
public class ShOrderController extends BaseController{

    @Autowired
    private OrderService orderService;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Value("${jwt.tokenHeader}")
    private String jwtTokenHeader;

    @Value("${jwt.tokenHead}")
    private String tokenHead;

    @Value("${order.pageSize}")
    private Integer pageSize;

    /***
     * 多条件搜索品牌数据
     * @return
     */
    @ApiOperation(value = "ShOrder条件查询",notes = "根据给定的条件实现查询")
    @PostMapping(value = "/search/{pageNum}" )
    public CommonResult<List<QueryOrderDto>> findList(@PathVariable Integer pageNum , @RequestBody OrderParam orderParam){
        // 根据token，获取Redis中的userId
        Long userId = Integer.toUnsignedLong((Integer) RedisUtil.get(jwtTokenUtil.getToken(request)));

        List<QueryOrderDto> list = orderService.findList(userId,pageNum,pageSize,orderParam);
        return CommonResult.success(list);
    }

    /***
     * 根据id查询订单详情
     * @return
     */
    @ApiOperation(value = "根据id查询订单详情",notes = "根据id查询订单详情")
    @GetMapping(value = "/{orderId}" )
    public CommonResult<OrderDetailDto> findList(@PathVariable Integer orderId){
        // 根据token，获取Redis中的userId
        Long userId = Integer.toUnsignedLong((Integer) RedisUtil.get(jwtTokenUtil.getToken(request)));

        OrderDetailDto orderDetailDto = orderService.findById(userId,orderId);
        return CommonResult.success(orderDetailDto);
    }

    @ApiOperation(value = "删除订单",notes = "删除订单")
    @ApiImplicitParam(paramType = "path", name = "orderId", value = "orderId", required = true, dataType = "Integer")
    @DeleteMapping(value = "/{orderId}" )
    public CommonResult delete(@PathVariable Integer orderId){
        // 根据token，获取Redis中的userId
        Long userId = Integer.toUnsignedLong((Integer) RedisUtil.get(jwtTokenUtil.getToken(request)));

        orderService.delete(userId, orderId);
        return CommonResult.success();
    }

    @ApiOperation(value = "修改订单信息",notes = "修改订单信息，只能修改地址和收货人的相关信息")
    @PutMapping
    public CommonResult update(@Validated @RequestBody ChangeOrderDto changeOrder){
        // 根据token，获取Redis中的userId
        Long userId = Integer.toUnsignedLong((Integer) RedisUtil.get(jwtTokenUtil.getToken(request)));
        orderService.update(userId,changeOrder);
        return CommonResult.success();
    }

    @ApiOperation(value = "添加订单",notes = "添加订单")
    @PostMapping
    public CommonResult add(@RequestBody OrderDto orderDto){
        // 根据token，获取Redis中的userId
        Long userId = Integer.toUnsignedLong((Integer) RedisUtil.get(jwtTokenUtil.getToken(request)));
        orderService.add(userId,orderDto);
        return CommonResult.success();
    }
}
